<!--
 * @Author: whq
 * @Date: 2025-03-06 16:37:58
 * @LastEditTime: 2025-03-12 11:21:30
 * @LastEditors: whq
 * @Description: 
 * @FilePath: /uniapp_build_mate_profit_mini/pagesUser/userAdd/index.vue
-->
<template>
  <view class="add-user-container">
    <uv-form :model="formData" ref="formRef" :rules="rules">
      <uv-form-item
        label="用户名"
        prop="username"
        labelPosition="top"
        :customStyle="{ width: '100%' }"
        labelWidth="200rpx"
      >
        <uv-input
          v-model="formData.username"
          placeholder="请输入用户名"
          border="bottom"
        />
      </uv-form-item>

      <uv-form-item
        label="密码"
        prop="password"
        labelPosition="top"
        :customStyle="{ width: '100%' }"
        labelWidth="200rpx"
      >
        <uv-input
          v-model="formData.password"
          type="password"
          placeholder="请输入密码"
          border="bottom"
        />
      </uv-form-item>

      <!--      <uv-form-item label="用户类型" prop="user_type">
        <uv-radio-group v-model="formData.user_type">
          <uv-radio label="普通用户" name="normal"></uv-radio>
          <uv-radio label="管理员" name="root"></uv-radio>
        </uv-radio-group>
      </uv-form-item> -->
    </uv-form>

    <view class="button-group">
      <uv-button
        type="primary"
        text="保存"
        @click="handleSubmit"
        :customStyle="{
          background: 'linear-gradient(260deg, #98C7F9 0%, #54D3ED 100%)',
        }"
      />
    </view>
  </view>
</template>

<script setup>
import { ref } from "vue";
import cloudConfig from "@/config/uniCloudConfig.js";
const uniCloudUsersManager = cloudConfig.getCloudObject("usersManager");
const formRef = ref(null);

const formData = ref({
  username: "",
  password: "",
  user_type: "normal",
  status: 1,
});

const rules = ref({
  username: [
    {
      required: true,
      message: "请输入用户名",
      trigger: ["blur", "change"],
    },
  ],
  password: [
    {
      required: true,
      message: "请输入密码",
      trigger: ["blur", "change"],
    },
  ],
  // user_type: [{
  // 	required: true,
  // 	message: "请选择用户类型",
  // 	trigger: ["blur", "change"],
  // }, ],
});

const handleSubmit = async () => {
  try {
    const valid = await formRef.value.validate();
    if (valid) {
      const res = await uniCloudUsersManager.addUser(formData.value);
      if (res.code === 0) {
        uni.showToast({
          title: "添加成功",
          icon: "success",
        });
        setTimeout(() => {
          uni.navigateBack();
        }, 1500);
      } else {
        uni.showToast({
          title: res.message,
          icon: "none",
        });
      }
    }
  } catch (error) {
    console.error(error);
  }
};
</script>

<style lang="scss" scoped>
.add-user-container {
  padding: 30rpx;

  .button-group {
    margin-top: 60rpx;
  }
}
</style>
